package com.ewhale.feitengguest.widget

import android.app.Dialog
import android.content.Context
import androidx.core.widget.addTextChangedListener

import com.ewhale.feitengguest.R
import com.simga.library.activity.MBaseActivity
import com.simga.library.utils.LogUtil
import com.simga.library.utils.glide.GlideUtil
import kotlinx.android.synthetic.main.dialog_increase_number.*
import kotlinx.android.synthetic.main.dialog_increase_prcie.*
import kotlinx.android.synthetic.main.dialog_increase_prcie.btn_commit
import kotlinx.android.synthetic.main.dialog_increase_prcie.et_name
import kotlinx.android.synthetic.main.dialog_increase_prcie.iv_close
import kotlinx.android.synthetic.main.dialog_increase_prcie.iv_cover
import kotlinx.android.synthetic.main.dialog_increase_prcie.tv_title

import java.math.BigDecimal

/**
 * 作者：SimGa
 * 日期：2020/2/26
 * 时间：11:04
 * 包名：com.ewhale.feitengguest.widget
 */
class IncreaseNumberDialog(context: Context) : Dialog(context, com.simga.library.R.style.Dialog) {
    private val activity: MBaseActivity<*>

    init {
        this.setCanceledOnTouchOutside(false)
        this.setCancelable(false)
        this.activity = context as MBaseActivity<*>
        setContentView(R.layout.dialog_increase_number)

        btn_commit.setOnClickListener {
            val number = et_name.text.toString()
            if (et_name.text.isEmpty()) {
                activity.showToast("请输入增加的人数")
                return@setOnClickListener
            }
            if (et_name.text.toString() == "0") {
                activity.showToast("新增人数必须大于0")
                return@setOnClickListener
            }
            if (listenter != null) {
                listenter!!.onClickCommit(number)
            }
            dismiss()
        }
        iv_reduce.setOnClickListener {
            var number = et_name.text.toString()
            if (number.isEmpty()) {
                return@setOnClickListener
            }
            if (number.toInt() == 0) {
                return@setOnClickListener
            }
            number = (number.toInt() - 1).toString()
            et_name.setText(number)
        }
        iv_add.setOnClickListener {
            var number = et_name.text.toString()
            if (number.isEmpty()) {
                et_name.setText("1")
                return@setOnClickListener
            }
            number = (number.toInt() + 1).toString()
            et_name.setText(number)
        }
        iv_close.setOnClickListener {
            dismiss()
        }
    }

    fun setData(img: String, title: String, surplus: Int) {
        GlideUtil.loadPicture(img, iv_cover, R.drawable.default_image)
        tv_title.text = title
        tv_surplus.text = surplus.toString()
    }

    private var listenter: onClickListenter? = null

    fun setListenter(listenter: onClickListenter) {
        this.listenter = listenter
    }

    interface onClickListenter {
        fun onClickCommit(number: String)
    }
}
